<?php
namespace Business\sms;

class YunTongXunSMS extends YunTongXunSDK
{
    //主帐号
    const ACCOUNT_SID   = '8a216da85a25f2dc015a3626667f03c0';
    //主帐号Token
    const ACCOUNT_TOKEN = 'e41aa7d0292a4938bccee15ee4ada6d0';
    //应用Id
    const APPID         = '8a216da85a25f2dc015a3626687503c6';

    //测试环境用
/*    public function getTemplate($ptype)
    {
        $list = [
            171333      => ['content'=>'【租付通】您的验证码是{1}，请于{2}内输入'],
            170556      => ['content'=>'【租付通】{1}，账单周期{2}，房号{3}，总金额为{4}，账单明细{5}'],
            170854      => ['content'=>'【租付通】{1}，用户{2}，您已成功缴费，账单周期{3}，房号{4}，总金额为{5}，账单明细{6}，缴费时间{7}'],
            170856      => ['content'=>'【租付通】{1}，付款方{2}，收款方{8}，周期{3}，房号{4}，总金额为{5}，账单明细{6}，缴费时间{7}'],
            170970      => ['content'=>'【租付通】{1}，付款方{2}，收款方{3}，周期{4}，房号{5}，总金额为{6}，账单明细{7}，缴费时间{8}'],
        ];
        return isset($list[$ptype]) ? $list[$ptype] : [];
    }*/

    public function __construct()
    {
        parent::__construct('app.cloopen.com', '8883', '2013-12-26');
        $this->setAccount(self::ACCOUNT_SID, self::ACCOUNT_TOKEN);
        $this->setAppId(self::APPID);
    }

    /**
     * 发送验证码
     * @param string $mobile
     * @param array $datas
     * @param int $tempId
     * @return bool
     */
    public function doSendSMS($mobile, $datas, $p_type)
    {
        if (strtolower(ENV)!='production') {
            zft_log('sms/data', "云通讯:|$mobile:|".json_encode($datas, JSON_UNESCAPED_UNICODE));
            return ['code'=>200,'content'=>json_encode($datas, JSON_UNESCAPED_UNICODE)];
        }

        $result     = parent::sendTemplateSMS($mobile, [$datas[0], $datas[1]], $p_type);
        if ($result == null || $result->statusCode!=0) {
            //TODO 添加错误处理逻辑
            zft_log('sms/fail', "云通讯发送短信失败|datas:" . json_encode($datas, JSON_UNESCAPED_UNICODE) . "errcode:{$result->statusCode},errmsg:{$result->statusMsg}");
            return ['code'=>401, 'msg'=>$result->statusCode . ':'. $result->statusMsg];
        } else {
            //TODO 添加成功处理逻辑
            return ['code'=>200,'content'=>json_encode($datas)];
        }
    }

    /**
     * 发送缴费短信
     * @author wuhao
     * @param string $mobile
     * @param array $datas
     * @param array $content
     * @param int $tplId
     * @return bool
     */
    public function sendPayNotice($mobile, $datas, $content,  $tplId)
    {
        if (strtolower(ENV)!='production') {//内网
            zft_log('sms/data', "云通讯:|$mobile:|" . json_encode($datas, JSON_UNESCAPED_UNICODE));
            return ['code'=>200,'content'=>json_encode($datas, JSON_UNESCAPED_UNICODE)];
        }

        //线上
        $result     = parent::sendTemplateSMS($mobile, [$datas[0],$datas[1],$datas[2],$datas[3],$datas[4]], $tplId);
        if ($result == null || $result->statusCode!=0) {
            //TODO 添加错误处理逻辑
            zft_log('sms/fail', "云通讯发送短信失败|datas:" . json_encode($datas, JSON_UNESCAPED_UNICODE) . "errcode:{$result->statusCode},errmsg:{$result->statusMsg}");
            return ['code'=>401, 'msg'=>$result->statusCode . ':'. $result->statusMsg];
        } else {
            //TODO 添加成功处理逻辑
            return ['code'=>200];
        }
    }


    /**
     * 发送到账短信
     * @author wuhao
     * @param string $mobile 发送对象
     * @param array $datas  内容
     * @param array $content 限制内容
     * @param int  $sendMember 发送对象 1=付款方 2=收款方
     * @param int $tplId 模板id
     * @return bool
     */
    public function sendIncomeNotice($mobile, $datas, $content,  $sendMember = 1, $tplId)
    {
        if (strtolower(ENV)!='production') {
            zft_log('sms/data', "云通讯:|$mobile:|".json_encode($datas, JSON_UNESCAPED_UNICODE));
            return ['code'=>200,'content'=>json_encode($datas, JSON_UNESCAPED_UNICODE)];
        }

        //线上
        if ($sendMember == 1) {
            $result     = parent::sendTemplateSMS($mobile, [$datas[0],$datas[6],$datas[1],$datas[8], $datas[2],$datas[3], $datas[4], $datas[5]], $tplId);
        } else {
            $result     = parent::sendTemplateSMS($mobile, [$datas[0],$datas[6],$datas[7],$datas[1], $datas[8], $datas[2], $datas[3], $datas[4], $datas[5]], $tplId);
        }

        if ($result == null || $result->statusCode!=0) {
            //TODO 添加错误处理逻辑
            zft_log('sms/fail', "云通讯发送短信失败|datas:" . json_encode($datas, JSON_UNESCAPED_UNICODE) . "errcode:{$result->statusCode},errmsg:{$result->statusMsg}");
            return ['code'=>401, 'msg'=>$result->statusCode . ':'. $result->statusMsg];
        } else {
            //TODO 添加成功处理逻辑
            return ['code'=>200];
        }
    }
}
